package com.xiaoxiang.live.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxiang.common.result.HttpResult;
import com.xiaoxiang.live.entity.Room;
import com.xiaoxiang.live.service.RoomService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * (Room)表控制层
 *
 * @author haha
 * @since 2022-03-28 13:02:08
 */
@RestController
@RequestMapping("room")
public class RoomController {
    /**
     * 服务对象
     */
    @Resource
    private RoomService roomService;


    @Value("${live.push-url}")
    private String pushUrl;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param room 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    public HttpResult selectAll(Page<Room> page, Room room) {
        return HttpResult.success(this.roomService.page(page, new QueryWrapper<>(room)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping
    public HttpResult selectOne(@RequestParam Long id) {
        return HttpResult.success(this.roomService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param room 实体对象
     * @return 新增结果
     */
    @PostMapping
    public HttpResult insert(@RequestBody Room room) {
        room.setHeat(0L);
        room.setLiveId(UUID.randomUUID().toString());
        room.setPushUrl(pushUrl + room.getName());
        room.setIsOpen(false);
        this.roomService.save(room);
        return HttpResult.success(room.getId());
    }

    /**
     * 修改数据
     *
     * @param room 实体对象
     * @return 修改结果
     */
    @PutMapping
    public HttpResult update(@RequestBody Room room) {
        return HttpResult.success(this.roomService.updateById(room));
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping
    public HttpResult delete(@RequestParam("id") Long id) {
        return HttpResult.success(this.roomService.removeById(id));
    }
}
